package com.molin.mapTest.hashMapTest;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class Demo2 {
    //HashMap练习，存储老师信息并遍历
    public static void main(String[] args) {
        Map<Teacher,String> teacherStringMap=new HashMap<Teacher,String>();
        teacherStringMap.put(new Teacher("zhangsan",23,8000F),"molin001");
        teacherStringMap.put(new Teacher("lisi",23,10000F),"molin002");
        teacherStringMap.put(new Teacher("wangwu",23,6000F),"molin003");
        System.out.println("方式1=================");
        Set<Teacher> keys=teacherStringMap.keySet();
        for (Teacher key : keys) {
            System.out.println(key+" --- "+ teacherStringMap.get(key));
        }

        System.out.println("方式2=================");
        Set<Map.Entry<Teacher,String>> kvs=teacherStringMap.entrySet();
        for (Map.Entry<Teacher, String> kv : kvs) {
            System.out.println(kv.getKey()+" --- "+kv.getValue());
        }
        System.out.println("方式3=================");
        teacherStringMap.forEach(
                (Teacher ts,String id)->{
                    System.out.println(ts+" --- "+id);
                }
        );

    }
}
class Teacher{
    private String name;
   private int age;
   private float salary;

    public Teacher() {
    }

    public Teacher(String name, int age, float salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Teacher teacher = (Teacher) o;
        return age == teacher.age &&
                Float.compare(teacher.salary, salary) == 0 &&
                Objects.equals(name, teacher.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, salary);
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getSalary() {
        return salary;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }
}
